package graph;

import java.util.HashSet;
import java.util.Set;

public class Node {

        int register;
        
        Graph mygraph;
        int mykey;

        public void setRegister(int reg){
                register=reg;
        } 
        
        int getRegister (){
                return register;
        }
        
        public Node(Graph g) {
                register = -1;
                mygraph = g;
                mykey = g.nodecount++;
                NodeList p = new NodeList(this, null);
                if (g.mylast == null)
                        g.mynodes = g.mylast = p;
                else
                        g.mylast = g.mylast.tail = p;
        }

        NodeList succs;
        NodeList preds;

        public NodeList succ() {
                return succs;
        }
        
        public Set<Node> getSuccSet() {
                Set<Node> result = new HashSet<Node>();
                NodeList aux = succs;
                while(aux.head != null) {
                        result.add(aux.head);
                        aux = aux.tail;
                }
                return result;
        }

        public NodeList pred() {
                return preds;
        }

        NodeList cat(NodeList a, NodeList b) {
                if (a == null)
                        return b;
                else
                        return new NodeList(a.head, cat(a.tail, b));
        }

        public NodeList adj() {
                return cat(succ(), pred());
        }

        int len(NodeList l) {
                int i = 0;
                for (NodeList p = l; p != null; p = p.tail)
                        i++;
                return i;
        }

        public int inDegree() {
                return len(pred());
        }

        public int outDegree() {
                return len(succ());
        }

        public int degree() {
                return inDegree() + outDegree();
        }

        public boolean goesTo(Node n) {
                return Graph.inList(n, succ());
        }

        public boolean comesFrom(Node n) {
                return Graph.inList(n, pred());
        }

        public boolean adj(Node n) {
                return goesTo(n) || comesFrom(n);
        }

        public String toString() {
                return String.valueOf(mykey);
        }

}